<?php

namespace App\Services;

use Illuminate\Support\Facades\Log;
use App\Models\PartnerUser;
use App\Models\MoneyPartner;
use App\Models\HallPartnerConfig;
use common\utils\SysCode;

/**
 * Created by PhpStorm.
 * User: SCF
 * Date: 2018/10/17
 * Time: 16:18
 */
class GameService
{
    // 第三方登陆
    public static function login($user, $gameType, $method, $platform = 0)
    {
        //登陆method
        if ($method == 'gameUrl') {
            $username = self::getPartnerUsername($user, $gameType);
            $data = [
                'apiName' => $gameType,
                'username' => $username,
                'password' => '123456',
                'isTrial' => 0,
                'platform' => $platform,
            ];
            $resposeData = self::rpcRequest('/api/login', 'POST', $data);
            // 成功
            if ($resposeData['code'] == 0) {
                if ($gameType == 'hc') {
                    $hcresponse = $resposeData['data'];
                    if ($platform == 1) {
                        return $hcresponse['h5_url'] . "&token=" . $hcresponse['token'];
                    } else {
                        return $hcresponse['pc_url'] . "&token=" . $hcresponse['token'];
                    }
                } else {
                    return $resposeData;
                }
            }
        }
        return false;
    }

    // 创建用户
    public static function createUser($user, $gameType)
    {
        $gameType = strtoupper($gameType);
        //创建子钱包
        $partner = HallPartnerConfig::where(['partner_name' => $gameType, 'status' => 1])->first();
        if (empty($partner)) {
            return error_exit(SysCode::INVALID_ARGUMENT);
        }
        //检测子钱包是否存在
        $checkChildWallet = MoneyPartner::where(['user_id' => $user['id']])->where(['partner_id' => $partner['id']])->first();
        if (empty($checkChildWallet)) {
            MoneyPartner::insert([
                'user_id' => $user['id'],
                'partner_id' => $partner['id'],
                'partner_name' => $gameType,
                'balance' => 0,
                'balance_before' => 0,
                'status' => 1
            ]);
        }

        //检测第三方用户名是否存在
        $username = strtolower(env('APP_ENV') . "_" . $gameType . "_" . HallDomainService::getHallID() . "_" . $user['id']);
        $checkPartnerName = PartnerUser::where(['user_id' => $user['id'], 'partner_id' => $partner['id']])->first();
        if (empty($checkPartnerName)) {
            PartnerUser::insert([
                'user_id' => $user['id'],
                'username' => $user['username'],
                'partner_username' => $username,
                'partner_id' => $partner['id'],
                'partner_name' => $gameType,
                'partner_password' => 123456,
            ]);
            //第三方注册
            self::register($username, $gameType);
        }
        return $username;
    }

    // 获取用户名
    public static function getPartnerUsername($user, $gameType)
    {
        $pu = PartnerUser::where(['user_id' => $user['id'], 'partner_name' => strtoupper($gameType)])->first();
        if ($pu) {
            return $pu->partner_username;
        }
        return static::createUser($user, $gameType);
    }

    // 注册
    public static function register($username, $gameType)
    {
        $data = [
            'apiName' => $gameType,
            'username' => $username,
            'password' => '123456',
            'isTrial' => 0,
        ];
        return self::rpcRequest('/api/register', 'POST', $data);
    }

    // 转账
    public static function transfer($user, $gameType, $orderNumber, $amount)
    {
        $username = self::getPartnerUsername($user, $gameType);
        $data = [
            'apiName' => $gameType,
            'username' => $username,
            'transactionNo' => $orderNumber,
            'amount' => $amount,
            'password' => 123456,
        ];

        return self::rpcRequest('/api/transfer', 'POST', $data);
    }

    // 获取余额
    public static function getBalance($user, $gameType)
    {
        $username = self::getPartnerUsername($user, $gameType);
        $data = [
            'apiName' => $gameType,
            'username' => $username,
            'password' => 123456
        ];
        return self::rpcRequest('/api/getBalance', 'GET', $data);
    }

    // RPC请求
    public static function rpcRequest($url, $method, $data)
    {
        return RpcService::rpcRequest($url, $method, $data);
    }
}